<template>
    <v-layout fill-height class="bg-white">
        <v-flex class="flex-auto col-center">

        </v-flex>
    </v-layout>
</template>

<script lang="ts">
  import {Component, Model, Vue} from 'vue-property-decorator'
  import User from "@/model/User"
  import ImgFile from "@/model/ImgFile"
  import RouterName from "@/const/RouterName"
  import CommonStatus from "@/const/CommonStatus"

  import {namespace} from "vuex-class"

  const userModule = namespace('user')

  @Component
  export default class Authentication extends Vue {
    @userModule.State('user') user: User
    //身份证正面文件
    idCardFrontImg: ImgFile = null
    //身份证反面文件
    // idCardBack: File = null


    previewImg({target}: any, type) {
      this.$img.getImgObj(target).then((imgObj) => {
        const imgSize = imgObj.file.size
        if (imgSize / 1024 / 1024 > 10) {
          this.$error('图片大小不能超过10MB！')
          return
        }
        if (type === this.$const.uploadType.frontIdCardImg) {
          this.idCardFrontImg = imgObj
        }
      })
    }

    cancelClick() {
      this.$routerTo(RouterName.mine)
    }

    authenticationIdCard() {
      if (!this.user.idCardStatus || this.user.idCardStatus === CommonStatus.init) {
        const frontFormData = new FormData()
        frontFormData.append("file", this.idCardFrontImg.file)
        frontFormData.append("idCardType", this.$const.uploadType.frontIdCardImg)
        this.$post('/upload/img', frontFormData).then(res => {
          // 上面两个请求都完成后，才执行这个回调方法
          this.$post('user/idCard/add', {
            frontIdCardImg: res.data.img,
          }).then(res => {
            this.$store.commit('user/setUser', res.data)
            this.$alert('提交认证，等待审核！').then(() => {
              this.$routerTo(RouterName.mine)
            })
          })
        })
      } else {
        this.$error("已经提交过认证申请，暂无法再次提交!")
      }

      /*const frontFormData = new FormData()
      frontFormData.append("file", this.idCardFront)
      frontFormData.append("idCardType", this.$const.uploadType.frontIdCardImg)
      const backFormData = new FormData()
      backFormData.append("file", this.idCardBack)
      frontFormData.append("idCardType", this.$const.backIdCard)
      this.$axios.all(
        [
          this.$post('/upload/img', frontFormData),
          this.$post('/upload/img', backFormData)
        ]
      )*/
    }
  }
</script>
